package com.huawei.hmsdemo10086.Activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.huawei.hmsdemo10086.Adapter.CarAdapter;
import com.huawei.hmsdemo10086.Adapter.GoodsAdapter;
import com.huawei.hmsdemo10086.Adapter.OrderAdapter;
import com.huawei.hmsdemo10086.Adapter.ShopAdapter;
import com.huawei.hmsdemo10086.Adapter.VideoAdapter;
import com.huawei.hmsdemo10086.POJO.Car;
import com.huawei.hmsdemo10086.POJO.Goods;
import com.huawei.hmsdemo10086.POJO.Result;
import com.huawei.hmsdemo10086.POJO.Shop;
import com.huawei.hmsdemo10086.R;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class ShopActivity extends AppCompatActivity {

    private List<Shop> datas;
    private ShopAdapter shopAdapter;
    private ListView listView;
    int flag;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop);
        flag = 0;
        listView = (ListView)this.findViewById(R.id.shop_list);
        datas = new ArrayList<Shop>();
        TextView mTitleTextView = (TextView) this.findViewById(R.id.text_title);
        mTitleTextView.setText("商店列表");
        Button back = (Button)this.findViewById(R.id.button_backward);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ShopActivity.this,MainActivity.class);
                startActivity(intent);
                finish();
            }
        });
        getShop();
        while(flag == 0)
        {
            System.out.println(1);
        }
        shopAdapter = new ShopAdapter(ShopActivity.this,datas);
        listView.setAdapter(shopAdapter);
        shopAdapter.notifyDataSetChanged();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(ShopActivity.this, GoodsActivity.class);
                intent.putExtra("shopid",datas.get(position).getShop_id());
//                NewsData tmp = datas.get(position);
//                datas.remove(position);
//                datas.add(0,tmp);
//                adapter.notifyDataSetChanged();

                startActivity(intent);//启动Activity
            }
        });
    }

    private void getShop()
    {
        String url = "http://10.0.2.2:8443/api/listShop";
        OkHttpClient mOkHttpClient = new OkHttpClient();

        Request request = new Request.Builder().url(url).get().build();

        mOkHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("测试", e+"");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                Gson gson = new Gson();
                String rep = response.body().string();
                System.out.println(rep.toString());

                Type type = new TypeToken<Result<List<Shop>>>(){}.getType();
                Result<List<Shop>> result = gson.fromJson(rep,type);
                for(int i = 0 ; i < result.getData().size() ; i++)
                {
                    System.out.println(result.getData().get(i).toString());
                }
                datas = result.getData();
                flag = 1;

            }
        });
    }


}